package com.llxz.blog.classify.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llxz.blog.classify.entity.Classify;
import com.llxz.blog.classify.entity.vo.One;
import com.llxz.blog.classify.entity.vo.Two;
import com.llxz.blog.classify.mapper.ClassifyMapper;
import com.llxz.blog.classify.service.ClassifyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 文章类别 服务实现类
 * </p>
 *
 * @author fw
 * @since
 */
@Service
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {
    @Autowired
    ClassifyMapper classifyMapper;

    public List<One> getAllOneTwo() {
        //1 查询所有一级分类  parentid = 0
        QueryWrapper<Classify> wrapperOne = new QueryWrapper<>();//新建QueryWrapper对象，类型为Classify对象，是需要查询的实体数据
        wrapperOne.eq("parent_id","0");
        List<Classify> oneList =this.list(wrapperOne);

        //2 查询所有二级分类  parentid != 0
        QueryWrapper<Classify> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<Classify> twoList = this.list(wrapperTwo);

        //创建list集合，用于存储最终封装数据
        List<One> finalList = new ArrayList<>();

        //3 封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值，
        //封装到要求的list集合里面 List<OneSubject> finalList
        for (int i = 0; i < oneList.size(); i++) { //遍历oneList集合
            //得到oneList每个classify对象
            Classify classify = oneList.get(i);
            //把classify里面值获取出来，放到One对象里面
            One one = new One();

            //classify值复制到对应one对象里面
            BeanUtils.copyProperties(classify,one);
            //多个OneSubject放到finalSubjectList里面
            finalList.add(one);

            //在一级分类循环遍历查询所有的二级分类
            //创建list集合封装每个一级分类的二级分类
            List<Two> twoFinalList = new ArrayList<>();
            //遍历二级分类list集合
            for (int m = 0; m < twoList.size(); m++) {
                //获取每个二级分类
                Classify tClassify = twoList.get(m);
                //判断二级分类parentid和一级分类id是否一样
                if(tClassify.getParentId().equals(one.getId())) {
                    //把tClassify值复制到TwoSubject里面，放到twoFinalSubjectList里面
                    Two two = new Two();
                    BeanUtils.copyProperties(tClassify,two);
                    twoFinalList.add(two);
                }
            }
            //把一级下面所有二级分类放到一级分类里面
            one.setChildren(twoFinalList);
        }
        return finalList;
    }

    @Override
    public void removeOneClassify(String title) {
        QueryWrapper<Classify> wrapper = new QueryWrapper<>();
        wrapper.eq("title",title).eq("parent_id","0");
        List<Classify> list = classifyMapper.selectList(wrapper);

        for (int i = 0; i < list.size(); i++){
            QueryWrapper<Classify> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",list.get(i).getId());
            classifyMapper.delete(wrapper1);
            classifyMapper.deleteById(list.get(i).getId());
        }

    }

    @Override
    public List<Classify> getClassify() {
        QueryWrapper<Classify> wrapperOne = new QueryWrapper<>();//新建QueryWrapper对象，类型为Classify对象，是需要查询的实体数据
        wrapperOne.eq("parent_id","0");
        List<Classify> list =this.list(wrapperOne);

        return list;
    }
}
